from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from .exts import db


class BaseModel(object):
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    start_date = db.Column(db.Date, default=datetime.now, onupdate=datetime.now, comment='更新时间')


class User(BaseModel, db.Model):
    __tablename__ = 'tb_user'
    user_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(32), nullable=False, unique=True)
    mobile = db.Column(db.String(11), unique=True, nullable=False)
    _password = db.Column(db.String(255), nullable=False)
    icon = db.Column(db.String(128), comment='用户头像')
    real_name = db.Column(db.String(32))
    real_idcard = db.Column(db.String(18), comment='身份证号码')

    houses = db.relationship('House', backref='user')
    orders = db.relationship('Order', backref='user')

    @property
    def password(self):
        raise AttributeError('Access Not Allowed')

    @password.setter
    def password(self, pwd):
        self._password = generate_password_hash(pwd)

    def check_pwd(self, pwd):
        return check_password_hash(self._password, pwd)

    # def to_dict(self):
    #     user_dict = {
    #         'user_id': self.user_id,
    #         'name': self.name,
    #         'mobile': self.mobile,
    #         'icon': constants.QINIU_DOMIN_PREFIX + self.icon if self.icon else "",
    #         'create_time': self.create_time.strftime("%Y-%m-%d %H:%M:%S")
    #     }
    #     return user_dict

    # def auth_to_dict(self):
    #     auth_dict = {
    #         'user_id': self.user_id,
    #         'real_name': self.real_name,
    #         'idcard': self.real_idcard
    #     }
    #     return auth_dict


house_facility = db.Table(
    'third_house_facility',
    db.Column('house_id', db.Integer, db.ForeignKey('tb_house.house_id'), primary_key=True, comment='关联House的facilities属性'),
    db.Column('facility_id', db.Integer, db.ForeignKey('tb_facility.facility_id'), primary_key=True, comment='关联Facility的houses属性')
)


class House(BaseModel, db.Model):
    __tablename__ = 'tb_house'
    house_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('tb_user.user_id'), comment='FK')
    title = db.Column(db.String(128), nullable=False)
    price = db.Column(db.Float, default=0.0)
    address = db.Column(db.String(255), default="", comment='地址')
    rooms = db.Column(db.Integer, default=1, comment='房间数量')
    acreage = db.Column(db.Float, default=0.0, comment='房屋面积')
    unit = db.Column(db.String(32), default="", comment='房屋单元')
    capacity = db.Column(db.Integer, default=1, comment='容纳人数')
    beds = db.Column(db.String(32), comment='房屋配置')
    deposit = db.Column(db.Float, default=0.0, comment='定金')
    min_days = db.Column(db.Integer, default=1, comment='最少入住天数')
    max_days = db.Column(db.Integer, default=0, comment='最多入住天数')

    order_count = db.Column(db.Integer, default=0, comment='预定该房订单数')

    index_image_url = db.Column(db.String(255), comment='房屋主图片路径')
    facilities = db.relationship('Facility', secondary=house_facility)

    images = db.relationship('HouseImage', backref='house')

    orders = db.relationship('Order', backref='house')
    area_id = db.Column(db.Integer, db.ForeignKey('tb_area.area_id'), nullable=False)


class Facility(BaseModel, db.Model):  # 设备
    __tablename__ = 'tb_facility'
    facility_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(128), nullable=False)


class Area(BaseModel, db.Model):
    __tablename__ = 'tb_area'
    area_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(128), nullable=False)
    houses = db.relationship('House', backref='area')

    def to_dict(self):
        area_dict = {
            'area_id': self.area_id,
            'area_name': self.name,
        }
        return area_dict


class Order(BaseModel, db.Model):
    __tablename__ = 'tb_order'
    order_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    begin_date = db.Column(db.DateTime, nullable=False, comment='预定的起始时间')
    end_date = db.Column(db.DateTime, nullable=False, comment='预定的结束时间')
    days = db.Column(db.Integer, nullable=False, comment='预定的总天数')
    price = db.Column(db.Float, nullable=False, comment='房屋单价')
    amount = db.Column(db.Float, nullable=False, comment='订单总金额')
    status = db.Column(db.Boolean, default=False)
    comments = db.Column(db.Enum(
        'WAIT_ACCEPT',  # 待接待
        'WAIT_PAYMENT',  # 待支付
        'PAID',  # 已支付
        'WAIT_COMMIT',  # 待评价
        'COMPLETED',  # 已完成
        'CANCELED',  # 已取消
        'REJECTED',  # 已拒绝
    ), default='WAIT_ACCESS', index=True)
    comment = db.Column(db.Text, comment='订单的评价信息或拒绝原因')
    trade_no = db.Column(db.String(129), comment='支付的交易编号')
    user_id = db.Column(db.Integer, db.ForeignKey('tb_user.user_id'), nullable=False)
    house_id = db.Column(db.Integer, db.ForeignKey('tb_house.house_id'), nullable=False)

    # def to_dict(self):
    #     order_dict = {
    #         'order_id': self.order_id,
    #         'title': self.house_id.title,
    #         'img_url': constants.QINIU_DOMIN_PREFIX + self.house_id.index_image_url if self.house_id.index_image_url else "",
    #         'start_date': self.begin_date.strftime("%Y-%m-%d"),
    #         'end_date': self.end_date.strftime("%Y-%m-%d %H:%:%S"),
    #         'ctime': self.create_time.strftime("%Y-%m-%d %H:%:%S"),
    #         'days': self.days,
    #         'amount': self.amount,
    #         'status': self.status,
    #         'comment': self.comment if self.comment else ""
    #     }
    #     return order_dict


class HouseImage(BaseModel, db.Model):
    __tablename__ = 'tb_house_image'
    image_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    url = db.Column(db.String(255))
    house_id = db.Column(db.Integer, db.ForeignKey('tb_house.house_id'), nullable=False)
